package com.angenela.imageshare.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.angenela.imageshare.MainActivity;
import com.angenela.imageshare.R;
import com.angenela.imageshare.dto.TagDTO;
import com.angenela.imageshare.dto.UserDTO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ChangePwd extends AppCompatActivity {

    private static final String USER = "http://122.9.152.182:8080/user/";

    EditText originPwd;
    EditText NewPwd;
    EditText NewPwdCheck;
    Button button;


    public Integer getUserId() {
        Resources resources = getResources();
        String spFileNameKey = resources.getString(R.string.shared_preferences_file_name);
        String idKey = resources.getString(R.string.login_account_id);
        SharedPreferences sharedPreferences =
                this.getSharedPreferences(spFileNameKey, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(idKey, 0);
    }

    public void Change(String NewPassword , String userId) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("password",NewPassword)
                .addFormDataPart("userId", userId)
                .build();

        Request request = new Request.Builder()
                .url(USER +"update/password")
                .post(requestBody)
                .build();

        Response response = okHttpClient.newCall(request).execute();


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        originPwd = findViewById(R.id.originPwd);
        NewPwd = findViewById(R.id.newPwd);
        NewPwdCheck = findViewById(R.id.NewPwdCheck);
        button = findViewById(R.id.Check);
        Intent intent = new Intent(this,LoginActivity.class);


        Resources resources = getResources();
        String spFileNameKey = resources.getString(R.string.shared_preferences_file_name);
        SharedPreferences sharedPreferences = getSharedPreferences(spFileNameKey, Context.MODE_PRIVATE);

        String PwdKey = resources.getString(R.string.login_account_password);

        String Pwd = sharedPreferences.getString(PwdKey,"");

        button.setOnClickListener(new View.OnClickListener() {

            AlertDialog.Builder dialogCreate = new AlertDialog.Builder(ChangePwd.this);
            @Override
            public void onClick(View v) {
                if(!originPwd.getText().toString().equals(Pwd)){
                    dialogCreate.setMessage("密码输入错误");
                    final AlertDialog dialog = dialogCreate.create();
                    dialog.show();
                }else if (!NewPwd.getText().toString().equals(NewPwdCheck.getText().toString())){
                    dialogCreate.setMessage("两次输入密码不匹配");
                    final AlertDialog dialog = dialogCreate.create();
                    dialog.show();
                }else if(Pwd.equals(NewPwd.getText().toString())){
                    dialogCreate.setMessage("修改后的密码不允许与之前一致");
                    final AlertDialog dialog = dialogCreate.create();
                    dialog.show();
                }else {
                    try {
                        Change(NewPwd.getText().toString(),getUserId().toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    dialogCreate.setMessage("修改成功，请重新登录");
                    final AlertDialog dialog = dialogCreate.create();
                    dialog.show();
                    try {
                        TimeUnit.SECONDS.sleep(4);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                    startActivity(intent);
                }
            }
        });
    }
}